<?php
/**
 * created by zzy
 * date: 2017/11/21 10:39
 */

namespace App\Http\Admin\System\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\Company;

class CompanyController extends Controller
{
    public function index()
    {
    }
    public function lists()
    {
        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $where          = [];
            $lists          = Company::getList( $where,'',$pageSize,$offset )->toArray();
            $count          = Company::getCount($where);
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $data_json      = json_encode($data);
            return $data_json;
        }
        return $this->view('lists' );
    }
    public function edit(){

        if (request()->isMethod('post')) {//数据提交
            $params = request()->all();
            //厂名称需要唯一
            $id                 = isset($params['id']) ? $params['id'] : 0;
            $title              = isset($params['title']) ? $params['title'] : '';
            if(!$title)
            {
                return $this->ajaxFail( '厂名称不能为空' );
            }
            $where              = ['title'=>$title];
            if($id>0)
            {
                $where['id']    = ['!=',$id];
            }
            $info               = Company::getInfo($where);
            if($info)
            {
                return $this->ajaxFail( '厂名称已存在' );
            }
            //添加或者修改
            $editResult         = Company::edit($params,'id');
            if($editResult && !isset($params['id']))
            {
                \App\Engine\WorkFlow::companyWorkFlowCreate($editResult);
            }
            if( $editResult === false ){
                return $this->ajaxFail( '失败' );
            } else {
                return $this->ajaxSuccess( '成功' );
            }
        }else{//编辑OR创建页面
            $params = request()->all();
            $id = isset($params['id']) ? $params['id'] : 0;
            $provinceList           = \App\Eloquent\Province::all();
            if($id){
                //获取原数据
                $info = \App\Eloquent\Ygt\Company::where(['id'=>$id])->first();
                if($info){
                    $info = $info->toArray();

                    ////添加图片地址
                    //LOGO
                    $info['logo_url'] = '';
                    if($info['logo']){
                        $tmpImgUrl = \App\Eloquent\Ygt\ImgUpload::getImgUrlById($info['logo']);
                        if($tmpImgUrl){
                            $info['logo_url'] = $tmpImgUrl;
                        }
                    }
                    //营业执照（五合一）
                    $info['business_licence_url'] = '';
                    if($info['business_licence']){
                        $tmpImgUrl = \App\Eloquent\Ygt\ImgUpload::getImgUrlById($info['business_licence']);
                        if($tmpImgUrl){
                            $info['business_licence_url'] = $tmpImgUrl;
                        }
                    }
                    //行业许可证
                    $info['industry_license_url'] = '';
                    if($info['industry_license']){
                        $tmpImgUrl = \App\Eloquent\Ygt\ImgUpload::getImgUrlById($info['industry_license']);
                        if($tmpImgUrl){
                            $info['industry_license_url'] = $tmpImgUrl;
                        }
                    }
                    //其他资质证书
                    $info['other_license_url'] = '';
                    if($info['other_license']){
                        $tmpImgUrl = \App\Eloquent\Ygt\ImgUpload::getImgUrlById($info['other_license']);
                        if($tmpImgUrl){
                            $info['other_license_url'] = $tmpImgUrl;
                        }
                    }


                }else{
                    $info = [];
                }
                $vdata = ['provinceList'=>$provinceList,'info' =>$info];
                return $this->view('edit',$vdata);

            }else{
                $vdata = ['provinceList'=>$provinceList];
                return $this->view('add',$vdata);
            }

        }

    }
    public function delete()
    {
        $companyId          = request('id',0);
        $where              = ['id'=>$companyId];
        $delResult          = Company::del($where);
        if($delResult === false){
            return $this->ajaxFail( '失败' );
        }else{
            return $this->ajaxSuccess( '成功' );
        }
    }
}
